﻿function AzureOnlineTableRecordsListView(context) {

    var self = this;

    var spinner = new SpinnerModel();

    var table = context.tables.table;

    var records = context.tables.records;

    var pager = new PagerModel(records);

    var tableSettings = context.tables.tableSettings;

    var filterInput = new AzureTableFilterExpressionInput();

    var filter = filterInput.filter;
    var appliedFilter = ko.observable('');

    var tableColumns = context.tables.tableColumns;
    var schema = context.tables.schema;

    var maxCount = ko.observable(100);

    var markers = context.tables.markers;

    var filters = ko.computed(function () {
        return new MRUList(tableSettings(), 'filters');
    });

    var queryBuilderFilter = ko.computed({
        read: function() {
            return filter();
        },
        write: function(val) {
            filter(val);
            applyFilter();
        }
    });

    filters.subscribe(function () {
        var f = filters().firstItem() || '';
        filter(f);
        appliedFilter(f);
    });


    function applyFilter() {
        var f = filter();
        appliedFilter(f);
        reload();

        var tableName = table();
        if (tableName) {
            filters().addItem(f);
        }
    }

    var fields = ko.computed(function () {
        return schema().fields();
    });

    function reload() {
        var tableName = table();
        if (!tableName) {
            records([]);
            return;
        }
        var api = context.api();
        spinner.enter();
        api.tables.query({
            table: tableName,
            top: maxCount(),
            filter: appliedFilter(),
            success: function (raw) {
                spinner.leave();
                var res = [];
                var $doc = $(raw);

                var $items = $doc.find('feed entry');
                $items.each(function () {
                    res.push(new AzureOnlineTableRecord($(this)));
                });

                records(res);
            },
            error: function () {
                spinner.leave();
                records([]);
                return 'notify';
            }
        });
    }

    function getRecordMarkers(rec, marks) {
        var res = {
            red: false,
            green: false,
            blue: false,
            yellow: false,
            gray: false,
            highlighted: false
        };
        var map = {};
        for (var i = 0; i < marks.length; i++) {
            var mark = marks[i];
            var expr = mark.expression();
            var clr = mark.color();
            if (!map[clr]) {
                map[clr] = true;
                res[clr] = false;
            }
            if (clr != 'none' && expr && expr.match(rec)) {
                res.highlighted = true;
                res[clr] |= true;
            }
        }
        return res;
    }

    function matches(rec, expr, highlight) {
        if (!expr || !highlight) return false;
        return expr.match(rec);
    }

    table.subscribe(function () {
        var api = context.api();
        if (api) {
            var cnt = table();
            reload(cnt);
        }
    });

    self.spinner = spinner;
    self.reload = reload;
    self.table = table;
    self.records = records;
    self.fields = fields;
    self.tableColumns = tableColumns;
    self.schema = schema;
    self.maxCount = maxCount;
    self.pager = pager;

    self.filters = filters;
    self.filter = filter;
    self.queryBuilderFilter = queryBuilderFilter;
    self.filterInput = filterInput;
    self.applyFilter = applyFilter;
    self.filterExpression = filterInput.expression;
    self.matches = matches;
    self.markers = markers;
    self.getRecordMarkers = getRecordMarkers;
}